package cn.fu.security.aes;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**   
* @Title: MyAES.java 
* @Package cn.fu.security.aes 
* @Description: 对称加密 ,通常用于通信，如ssh
* @author zhangxiaofu   
* @Company www.viewhigh.com
* @date 2016年8月21日 下午3:42:34 
* @version V1.0   
*/
public class MyAES {
private static String src = "99fu";

public static void main(String[] args) {
	bcAES();
	
}

public static void bcAES(){
	try {
		Security.addProvider(new BouncyCastleProvider());
		//生成key
		KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
		keyGenerator.init(128);//长度
//		keyGenerator.init(new SecureRandom());
		SecretKey secretKey = keyGenerator.generateKey();
		byte[] keyBytes = secretKey.getEncoded();
	
	
		//key转换
		SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
	
		//加密
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] result = cipher.doFinal(src.getBytes());
		System.err.println("bcAES:" + Base64.encodeBase64String(result));
		
		
		//解密
		cipher.init(Cipher.DECRYPT_MODE, key);
		result = cipher.doFinal(result);
		System.err.println("bc aes desrypt:" + new String(result));
	} catch (Exception e) {
	}
	
	
	
	
}
}

